package com.soufang.soufangdemo.controller;

import com.soufang.soufangdemo.base.ApiResponse;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

// OOP 封装，功能单一性
@RestController
public class IndexController {
    private final StringRedisTemplate template;

    public IndexController(StringRedisTemplate template) {
        this.template = template;
    }

    @GetMapping("/")
    public String index() {
        return "hello world";
    }

    @GetMapping("/set")
    public ApiResponse<Object> setKV(String key, String value) {
        template.opsForValue().set(key, value);
        return ApiResponse.success();
    }

    @GetMapping("/get")
    public ApiResponse<String> getV(String key) {
        String value = template.opsForValue().get(key);
        return ApiResponse.success(value);
    }
}
